//
//  MFSwiftUITools.swift
//  mfmm
//
//  Created by ming on 2017/7/24.
//  Copyright © 2017年 MF. All rights reserved.
//

import UIKit
let PF_Regular = "PingFangSC-Regular"
let PF_Medium  = "PingFangSC-Medium"
let MAIN_COLOR = UIColor(red: 2/255.0, green: 199/255.0, blue: 181/255.0, alpha: 1.0)
let KYP_333333_COLOR = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
let KYP_999999_COLOR = UIColor(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
let KYP_666666_COLOR = UIColor(red: 102/255.0, green: 102/255.0, blue: 102/255.0, alpha: 1.0)
let TABLE_BACKGROUND_COLOR =  UIColor(red: 240/255.0, green: 241/255.0, blue: 245/255.0, alpha: 1.0)
let KYP_f2f2f2_COLOR =  UIColor(red: 247/255.0, green: 248/255.0, blue: 249/255.0, alpha: 1.0)

let TABLE_LINE_COLOR =  UIColor(red: 239/255.0, green: 240/255.0, blue: 241/255.0, alpha: 1.0)

let Home_BTN_COLOR =  UIColor(red: 210/255.0, green: 210/255.0, blue: 210/255.0, alpha: 1.0)

let kStatusBarHeight = UIApplication.shared.statusBarFrame.size.height
let kNavBarHeight : CGFloat = 44
let kBottomSafeHeight = PublicTools.IsiPhoneX() ? 34 : 0
let kTabBarHeight = PublicTools.IsiPhoneX() ? 83 : 49
let kTopHeight = kStatusBarHeight + kNavBarHeight
let rateOfWidth = (SCREENW)/375

let SCREENW = UIScreen.main.bounds.size.width
let SCREENH = UIScreen.main.bounds.size.height

func RGBColor(r :CGFloat ,g:CGFloat,b:CGFloat) ->UIColor{
    return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1)
}

class PublicTools: NSObject {
//    static func formatTimeWithString(time : String) -> String{
//        let dateformatter = DateFormatter()
//        dateformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//        let date = dateformatter.date(from: time)
//        let datestr = dateformatter.string(from: date!)
//        return datestr
//    }
    
    
    static func compressImg(image : UIImage) -> UIImage{
        let newSize:CGSize = CGSize.init(width: 375, height: 375)
        UIGraphicsBeginImageContext(newSize)
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    
    
    }
    /**
     alterController 两个按钮 处理otherBtn事件
     
     - parameter currentVC: 当前控制器
     - parameter meg:       提示消息
     - parameter cancelBtn: 取消按钮
     - parameter otherBtn:  其他按钮
     - parameter handler:   其他按钮处理事件
     */
    static func showAlert(currentVC:UIViewController?, meg:String, cancelBtn:String, otherBtn:String?, handler:((UIAlertAction) -> Void)?){
        //        guard let vc = self.getCurrentVC() else{ return }
        var rootVC = currentVC
        if currentVC == nil {
            rootVC = UIApplication.shared.keyWindow?.rootViewController
        }
        
        let alertController = UIAlertController(title:"",
                                                message:meg ,
                                                preferredStyle: .alert)
        let cancelAction = UIAlertAction(title:cancelBtn, style: .cancel, handler:nil)
        
        alertController.addAction(cancelAction)
        
        if otherBtn != nil{
            let settingsAction = UIAlertAction(title: otherBtn, style: .default, handler: { (action) -> Void in
                handler?(action)
            })
            alertController.addAction(settingsAction)
        }
        if rootVC != nil {
            rootVC!.present(alertController, animated: true, completion: nil)

        }
    }
    ///颜色
    static func hexadecimalColor(hexadecimal:String)->UIColor{
        var cstr = hexadecimal.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
        if(cstr.length < 6){
            return UIColor.clear;
        }
        if(cstr.hasPrefix("0X")){
            cstr = cstr.substring(from: 2) as NSString
        }
        if(cstr.hasPrefix("#")){
            cstr = cstr.substring(from: 1) as NSString
        }
        if(cstr.length != 6){
            return UIColor.clear;
        }
        var range = NSRange.init()
        range.location = 0
        range.length = 2
        //r
        let rStr = cstr.substring(with: range);
        //g
        range.location = 2;
        let gStr = cstr.substring(with: range)
        //b
        range.location = 4;
        let bStr = cstr.substring(with: range)
        var r :UInt32 = 0x0;
        var g :UInt32 = 0x0;
        var b :UInt32 = 0x0;
        Scanner.init(string: rStr).scanHexInt32(&r);
        Scanner.init(string: gStr).scanHexInt32(&g);
        Scanner.init(string: bStr).scanHexInt32(&b);
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1);
    }
    
    class func IsiPhoneX() -> Bool {
        if UIScreen.main.bounds.height == 812 || UIScreen.main.bounds.height == 896{
            return true
        }
        return false
    }
    //添加阴影
    class func addTheShadow(view:UIView) -> Void{
        let rect = view.bounds
        view.layer.shadowOpacity = 0.15
        view.layer.shadowRadius = 4.0
        view.layer.shadowOffset = CGSize(width: 0, height: 0)
        view.layer.shadowPath = UIBezierPath(rect: rect).cgPath
    }
 
    //MARK:-导航条左侧按钮
    class func SLeftNavBarButtonWith(title: String? = nil,str_img:String? = nil,action:Selector,controller:UIViewController? = nil) -> UIButton {
        let btn = PublicTools.SButtonWith(title: title, str_img: str_img, controller: controller, action: action)
        btn.contentHorizontalAlignment = .left
        controller?.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: btn)
        return btn
    }
    //MARK:-导航条右侧按钮
    class func SRightNavBarButtonWith(title: String? = nil,str_img:String? = nil,action:Selector,controller:UIViewController? = nil) -> UIButton {
        let btn = PublicTools.SButtonWith(title: title, str_img: str_img,controller: controller, action: action)
        btn.contentHorizontalAlignment = .right

        controller?.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: btn)
        return btn
    }
    //MARK:-
    class func SButtonWith(title: String? = nil,str_img:String? = nil,controller:UIViewController? = nil,action:Selector) -> UIButton  {
        let btn = UIButton(type: .custom)
//        btn.backgroundColor = .red
        if title != nil {
            btn.setTitle(" " + title!, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }
        if str_img != nil {
            btn.setImage(UIImage.init(named: str_img!), for: .normal)
        }
        btn.addTarget(controller, action: action, for: .touchUpInside)
        btn.sizeToFit()
        var rect = btn.frame;
        if rect.size.width < 40 {
            rect.size.width = 40;
            rect.size.height = 40;
            btn.frame = rect
        }

        
        return btn
    }
    
   class func getTheBoolWith(key:String) -> Bool{
        let userInfo = UserDefaults.standard
        return userInfo.bool(forKey:key)
    }
   class func saveTheTutorialIsShow(key:String) -> Void {
        let userInfo = UserDefaults.standard
        userInfo.set(true, forKey: key)
        userInfo.synchronize()
    }
    class func currentVersionIsInReView() -> Bool{
        let userInfo = UserDefaults.standard
        let reviewVersion = userInfo.object(forKey: ReviewVersion)
        if reviewVersion == nil {
            return true
        }
        if let version = reviewVersion{
            let infoDict = Bundle.main.infoDictionary
            let currentVersion = infoDict!["CFBundleShortVersionString"] as! String
            if version as! String == currentVersion{
                return true
            }
        }
       
        return false
    }
    
}
// 下标截取任意位置的便捷方法
extension String {
    
//    var length: Int {
//        return self.characters.count
//    }
    
    subscript (i: Int) -> String {
        return self[i ..< i + 1]
    }
    
    func substring(fromIndex: Int) -> String {
        return self[min(fromIndex, length) ..< length]
    }
    
    func substring(toIndex: Int) -> String {
        return self[0 ..< max(0, toIndex)]
    }
  
    subscript (r: Range<Int>) -> String {
        let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)), upper: min(length, max(0, r.upperBound))))
        let start = index(startIndex, offsetBy: range.lowerBound)
        let end = index(start, offsetBy: range.upperBound - range.lowerBound)
        return String(self[start ..< end])
    }

    
    
}



